package com.gym.openapi.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.gym.openapi.common.ResponseResult;
import com.gym.openapi.common.ResultTable;
import com.gym.openapi.dao.AdminUserMapper;
import com.gym.openapi.entity.AdminUser;
import com.gym.openapi.services.AdminUserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ming
 * 2021/11/23
 * description:
 */
@Service
public class AdminUserServicesImpl implements AdminUserServices {

    @Autowired
    private AdminUserMapper userMapper;

    /**
     *@author:ming
     *@Date: 2021/11/23
     *@description:查询用户是否存在
     */
    @Override
    public AdminUser getUserByEmail(String email) {
        if (StringUtil.isNotEmpty(email)) {
            AdminUser userByname = userMapper.getUserByEmail(email);
            if (userByname != null) {
                return userByname;
            }
        }
        return null;
    }
    /**
     * @author:ming
     * @Date: 2021/11/23
     * @description:登录业务
     */
    @Override
    public AdminUser doLogin(String email, String password) {
        if (StringUtil.isNotEmpty(email) && StringUtil.isNotEmpty(password)) {
            AdminUser user = userMapper.getUserByEmail(email);
            if (user != null && user.getPassword().equals(password)) {
                return user;
            }
        }
        return null;
    }

    /**
     *@author:ming
     *@Date: 2021/11/25
     *@description:用户界面管理，查询搜索
     */
    @Override
    public ResultTable table(Integer page, Integer limit, String realName, String email, Integer status) {
        PageHelper.startPage(page, limit);
        List<AdminUser> users = userMapper.table(realName, email, status);
        PageInfo<AdminUser> pi = new PageInfo(users);

        return new ResultTable(0, "msg", (int) pi.getTotal(), pi.getList());
    }

    /**
     *@author:ming
     *@Date: 2021/11/25
     *@description:根据中间表查找用户id，信息
     */
    @Override
    public List<Integer> getUserRole(Integer userId) {
        if (userId > 0) {
            return userMapper.getUserRole(userId);
        }
        return null;
    }
/**
 *@author:ming
 *@Date: 2021/11/25
 *@description:类似授权信息
 * 删除role user 中间的所有 userid的数据
 * 进行批量新增
 */
    @Override
    public ResponseResult assignRole(Integer userId, Integer[] roleIds) {
        int i = userMapper.delUserRole(userId);
        int i2 = userMapper.addUserRole(userId, roleIds);
        return ResponseResult.getSuccess("批量删除成功");
    }

    /**
     *@author:ming
     *@Date: 2021/11/25
     *@description:删除
     */
    @Override
    public ResponseResult delUsers(Integer[] ids) {
        int i = userMapper.delUsers(ids);
        return ResponseResult.getSuccess("删除单个成功");
    }
    /**
     *@author:ming
     *@Date: 2021/11/25
     *@description:添加
     */
    @Override
    public ResponseResult addUser(AdminUser user) {
        if (user != null) {
            int i = userMapper.insertSelective(user);
            if (i > 0) {
                return ResponseResult.getSuccess("添加了个锤子");
            }
        }
        return ResponseResult.getFail("添加啥玩意？");
    }

    /**
     *@author:ming
     *@Date: 2021/11/25
     *@description:修改
     */
    @Override
    public ResponseResult updateUser(AdminUser user) {
        if (user != null) {
            int i = userMapper.updateByPrimaryKeySelective(user);
            if (i > 0) {
                return ResponseResult.getSuccess("改好咯");
            }
        }
        return ResponseResult.getFail("修改啥玩意？");
    }


}
